﻿//设置右键菜单
jQuery.extend({
    ContextMenuManager: {
        hasInit: false,
        allContextMenus: null,
        currentContextMenu: null,
        Init: function() {
            if ($.ContextMenuManager.allContextMenus == null) {
                $.ContextMenuManager.allContextMenus = new Array();
            }
            $.ContextMenuManager.hasInit = true;
        },
        FindContextMenu: function(targetClass) {
            var curMenu = null;
            $($.ContextMenuManager.allContextMenus).each(function(i, n) {
                if (n.props.targetClass == targetClass) {
                    curMenu = n;
                    return;
                }
            });
            return curMenu;
        },
        hideContextMenus: function() {
            $($.ContextMenuManager.allContextMenus).each(function(i, n) {
                $(n.menuObj).hide();
                if (n.hideFunc != null)
                    n.hideFunc(n.target);
            });
        },
        CreateContextMenu: function(props, events, showFuc, hideFunc) {
            var newMenu = new $.ContextMenuManager.ContextMenu(props, events, showFuc, hideFunc);
            newMenu.buildContextMenu();
            $.ContextMenuManager.allContextMenus.push(newMenu);
        },
        ContextMenu: function(props, events, showFuc, hideFunc) {
            this.props = props;
            this.events = events;
            this.showFunc = showFuc;
            this.hideFunc = hideFunc;
            this.target = null;
            this.menuObj = $("#" + this.props.menuID);
            this.targetClass = this.props.targetClass;
            if ($.ContextMenuManager.hasInit == false)
                $.ContextMenuManager.Init();

            $.ContextMenuManager.ContextMenu.prototype.buildContextMenu = function() {
                var eventFunc = this.events;
                $(document).bind("contextmenu", function(evt) {
                    try {
                        if ($.ContextMenuManager.currentContextMenu == null || $.ContextMenuManager.currentContextMenu.currentObj != evt.target) {
                            $.ContextMenuManager.currentContextMenu = $.ContextMenuManager.FindContextMenu(evt.target.className);
                        }
                        var menusMangager = $.ContextMenuManager;
                        var cContextMenu = menusMangager.currentContextMenu;
                        menusMangager.hideContextMenus();
                        if (cContextMenu != null) {
                            eventFunc = cContextMenu.events;
                            cContextMenu.target = evt.target;
                            var allMenuItems = $(cContextMenu.menuObj).find("li.MenuItem");
                            allMenuItems.each(function(i, n) {
//                                $(n).css("listStyleType", "none").one("click", function() {
//                                    menusMangager.hideContextMenus();
//                                    eventFunc.bindings[this.id](evt.target);
//                                    return false;
//                                });
                                n.onclick = function() {
                                    menusMangager.hideContextMenus();
                                    eventFunc.bindings[this.id](evt.target);
                                    return false;
                                };
                            });
                            var _bodyWidth = $(document).width();
                            var _bodyHeight = $(document).height();
                            var _mWidth = cContextMenu.menuObj.width();
                            var _mHeight = 120;
                            cContextMenu.menuObj.css("left", evt.clientX + "px"); // = ((parseInt(getX(evt)) + parseInt(_mWidth)) > parseInt(_bodyWidth) ? (parseInt(getX(evt)) - parseInt(_mWidth)) : getX(evt)) + "px";
                            cContextMenu.menuObj.css("top", evt.clientY + "px"); //((parseInt(getY(evt)) + parseInt(_mHeight)) > parseInt(_bodyHeight) ? (parseInt(getY(evt)) - parseInt(_mHeight) + 20) : getY(evt)) + "px";
                            cContextMenu.menuObj.show();
                            if (cContextMenu.showFunc != null)
                                cContextMenu.showFunc(evt.target);
                        }
                        else {
                            menusMangager.hideContextMenus();
                        }
                    } catch (e) { }
                    return false;
                }).bind("click", function() { $.ContextMenuManager.hideContextMenus(); });
            }
        }
    }
});

$(document).ready(function() {
    $.ContextMenuManager.Init();
});

 